<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="orgName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
                    style="margin-bottom: 20px"/>
        </div>
        <div class="head-container">
          <el-card class="box-card" shadow="never" body-style="width:100%;height:550px;overflow:auto">
            <el-tree :props="defaultProps" :expand-on-click-node="false" lazy :load="nodeExpand"
                     :filter-node-method="filterNode" style="height:100%"
                     ref="tree" highlight-current @node-click="handleNodeClick"/>
          </el-card>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <!-- 搜索工作栏 -->
        <el-row>
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                   label-width="68px">

            <el-col :span="24" :xs="24">
              <el-form-item label="编码" prop="code">
                <el-input v-model="queryParams.code" placeholder="请输入编码" clearable @keyup.enter.native="handleQuery"/>
              </el-form-item>
              <el-form-item label="名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="24" :xs="24">
              <el-form-item label=" " prop="showDisabled">
                <el-checkbox @change="showDisabled">显示已禁用组织</el-checkbox>
              </el-form-item>
              <el-form-item label="" prop="status">
                <el-checkbox @change="showVirtual">显示虚拟组织</el-checkbox>
              </el-form-item>
              <el-form-item label="" prop="showAllChildrenOrg">
                <el-checkbox @change="showAllChildrenOrg">显示所有下级组织</el-checkbox>
              </el-form-item>
              <el-form-item label="">
                <el-checkbox @change="showOrg">组织</el-checkbox>
              </el-form-item>
              <el-form-item label="" prop="showCenter">
                <el-checkbox @change="showCenter">中心</el-checkbox>
              </el-form-item>
              <el-form-item label="" prop="showDept">
                <el-checkbox @change="showDept">部门</el-checkbox>
              </el-form-item>
              <el-form-item label="" prop="showPos">
                <el-checkbox @change="showPos">岗位</el-checkbox>
              </el-form-item>
              <el-form-item label="" prop="showPsm">
                <el-checkbox @change="showPsm">人员</el-checkbox>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <!-- 操作工具栏 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddOrg"
                       v-if="!!currentOrgTreeData.orgKindId && currentOrgTreeData.orgKindId != positionKindCode "
                       v-hasPermi="['sa:org:create']">添加机构
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddDept"
                       v-if="!!currentOrgTreeData.orgKindId && currentOrgTreeData.orgKindId != positionKindCode && currentOrgTreeData.orgKindId != rootKindCode"
                       v-hasPermi="['sa:org:create']">添加部门
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddPos"
                       v-if="currentOrgTreeData.orgKindId == centerKindCode || currentOrgTreeData.orgKindId == deptKindCode "
                       v-hasPermi="['sa:org:create']">添加岗位
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddPsm"
                       v-if="currentOrgTreeData.orgKindId == positionKindCode "
                       v-hasPermi="['sa:org:create']">添加人员
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAllocationPsm"
                       v-if="currentOrgTreeData.orgKindId == positionKindCode "
                       v-hasPermi="['sa:org:create']">分配人员
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                       :loading="exportLoading"
                       v-hasPermi="['sa:org:export']">导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 列表 -->
        <el-table v-loading="loading" :data="list" border style="width: 100%">
          <!--          <el-table-column label="ID" align="center" prop="id"/>-->
          <el-table-column fixed label="编码" align="center" prop="code" width="150px"/>
          <el-table-column fixed label="名称" align="left" prop="name" width="200px"/>
          <el-table-column label="路径" align="left" prop="fullName" width="200px" show-overflow-tooltip/>
          <el-table-column label="机构类型" align="center" prop="orgKindId">
            <template v-slot="scope">
              {{ getOrgKindName(scope.row.orgKindId) }}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template v-slot="scope">
              <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1"
                         @change="handleStatusChange(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column label="排序号" align="center" prop="sequence"/>
          <el-table-column label="是否中心" align="center" prop="isCenter"/>
          <el-table-column label="公司名称" align="center" prop="orgName" width="200px"/>
          <el-table-column label="中心名称" align="center" prop="centerName" width="200px"/>
          <el-table-column label="部门名称" align="center" prop="deptName" width="200px"/>
          <el-table-column label="岗位名称" align="center" prop="positionName" width="200px"/>
          <el-table-column label="虚拟组织" align="center" prop="isVirtual" width="80px">
            <template v-slot="scope">
              {{ scope.row.isVirtual == '1' ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150px" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['sa:org:update']">修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['sa:org:delete']">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>

      </el-col>
    </el-row>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="全名称" prop="longName">
              <el-input v-model="form.longName" placeholder="请输入长名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.orgKindId == deptKindCode">
            <el-form-item label="是否中心" prop="isVirtual">
              <el-select v-model="form.isCenter" placeholder="请输入是否虚拟组织">
                <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label"
                           :value="parseInt(dict.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否虚拟组织" prop="isVirtual">
              <el-select v-model="form.isVirtual" placeholder="请输入是否虚拟组织">
                <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label"
                           :value="parseInt(dict.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织类型" prop="orgKindName">
              <el-input v-model="form.orgKindName" disabled placeholder="组织类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序号" prop="sequence">
              <el-input v-model="form.sequence" placeholder="请输入排序号"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述">
              <el-input type="textarea" v-model="form.description"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrgForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 对话框(人员  添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="personFormOpen" width="1200px" v-dialogDrag append-to-body>
      <el-form ref="personForm" :model="personForm" :rules="personRules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级" prop="parentFullName">
              <el-input v-model="personForm.parentFullName" readonly placeholder="请输入编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码" prop="code">
              <el-input v-model="personForm.code" placeholder="请输入编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="名称" prop="name">
              <el-input v-model="personForm.name" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="登录名" prop="loginName">
              <el-input v-model="personForm.loginName" placeholder="请输入登录名"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="personForm.idCard" placeholder="请输入身份证号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码时限" prop="passwordTimeLimit">
              <el-input v-model="personForm.passwordTimeLimit" placeholder="请输入密码时限"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码修改时间" prop="passwordModifyTime">
              <el-input v-model="personForm.passwordModifyTime" placeholder="" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序号" prop="sequence">
              <el-input v-model="personForm.sequence" placeholder="请输入排序号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="personForm.status">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                          :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="personForm.sex">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
                          :key="dict.value" :label="dict.value">{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker clearable v-model="personForm.birthday" type="date" value-format="timestamp"
                              placeholder="选择出生日期间" style="width:100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="参加工作日期" prop="joinDate">
              <el-date-picker clearable v-model="personForm.joinDate" type="date" value-format="timestamp"
                              placeholder="选择参加工作日期" style="width:100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生地" prop="homePlace">
              <el-input v-model="personForm.homePlace" placeholder="请输入出生地"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学历" prop="degree">
              <el-input v-model="personForm.degree" placeholder="请输入学历"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毕业院校" prop="graduateSchool">
              <el-input v-model="personForm.graduateSchool" placeholder="请输入毕业院校"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="专业" prop="speciality">
              <el-input v-model="personForm.speciality" placeholder="请输入专业"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学年制" prop="schoolLength">
              <el-input v-model="personForm.schoolLength" placeholder="请输入学年制"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="职称" prop="title">
              <el-input v-model="personForm.title" placeholder="请输入职称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="婚姻状况" prop="marriage">
              <el-input v-model="personForm.marriage" placeholder="请输入婚姻状况"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="证件号码" prop="cardNo">
              <el-input v-model="personForm.cardNo" placeholder="请输入证件号码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家庭住址" prop="familyAddress">
              <el-input v-model="personForm.familyAddress" placeholder="请输入家庭住址"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮编" prop="zip">
              <el-input v-model="personForm.zip" placeholder="请输入邮编"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="QQ" prop="qq">
              <el-input v-model="personForm.qq" placeholder="请输入QQ"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="移动电话" prop="mobilePhone">
              <el-input v-model="personForm.mobilePhone" placeholder="请输入移动电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="英文名称" prop="englishName">
              <el-input v-model="personForm.englishName" placeholder="请输入英文名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="微信" prop="appCode">
              <el-input v-model="personForm.appCode" placeholder="请输入微信"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <el-input v-model="personForm.description" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPersonForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="人员选择" :visible.sync="openUserPicker" width="1200px" append-to-body style="text-align: center;">
      <SaUserPicker @complete="getUserList" @close="openUserPicker = false"/>
    </el-dialog>
  </div>
</template>

<script>
import {
  createOrg,
  updateOrg,
  deleteOrg,
  getOrg,
  getOrgPage,
  exportOrgExcel,
  queryOrgTree,
  changeOrgStatus,
  batchCreatePsm
} from "@/api/sa/org";
import {createPerson, updatePerson, deletePerson, getPerson, getPersonPage, exportPersonExcel} from "@/api/sa/person";
import Editor from '@/components/Editor';
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import {CommonStatusEnum, OrgKindEnum, YesOrNo} from "@/utils/constants";
import {changeUserStatus} from "@/api/system/user";
import SaUserPicker from '@/components/SaPersonPicker';

export default {
  name: "Org",
  components: {
    Editor, SaUserPicker
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组织机构管理列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 机构搜索
      orgName: '',
      orgOptions: undefined,
      defaultProps: {
        children: "children",
        label: "name"
      },
      currentOrgTreeId: '',
      currentOrgTreeData: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: 0, // 有效
        isVirtual: 0 //非虚拟组织
      },
      queryOrgKindIds: {}, //查询的机构类型
      // 表单参数
      form: {
        isCenter: YesOrNo.NO.value
      },
      // 表单校验
      rules: {
        // version: [{required: true, message: "版本号不能为空", trigger: "blur"}],
      },
      rootKindCode: OrgKindEnum.Root.code,
      orgKindCode: OrgKindEnum.Org.code,
      deptKindCode: OrgKindEnum.Dept.code,
      centerKindCode: OrgKindEnum.Center.code,
      positionKindCode: OrgKindEnum.Position.code,
      statusDictDatas: [YesOrNo.YES, YesOrNo.NO],

      personForm: {},
      personFormOpen: false,
      personRules: {
        code: [{required: true, message: "编码不能为空", trigger: "blur"}],
        name: [{required: true, message: "名称不能为空", trigger: "blur"}],
        loginName: [{required: true, message: "登录名不能为空", trigger: "blur"}]
      },
      openUserPicker: false
    };
  },
  created() {

  },

  methods: {
    // 显示机构类型
    getOrgKindName(orgKindId) {
      for (let item in OrgKindEnum) {
        if (orgKindId == OrgKindEnum[item].code) {
          return OrgKindEnum[item].name;
        }
      }
      return "错误类型"
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },

    //懒加载树节点，
    nodeExpand(node, resolve) {
      let id = node.level == 0 ? '' : node.data.id
      queryOrgTree(id).then(response => {
        resolve(response.data);
        if (node.level == 0) {
          this.queryParams.parentId = response.data[0].id;
          this.getList();
        }
        this.loading = false;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (!!this.queryParams.fullId) {
        this.queryParams.fullId = data.fullId || "/";
      } else {
        this.queryParams.parentId = data.id;
      }
      this.currentOrgTreeId = data.id;
      this.currentOrgTreeData = data;
      this.getList();
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getOrgPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.personFormOpen = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        code: undefined,
        name: undefined,
        longName: undefined,
        orgKindName: undefined,
        description: undefined,
        orgKindId: undefined,
        sequence: undefined,
        isCenter: YesOrNo.NO.value,
        status: CommonStatusEnum.ENABLE,
        isVirtual: YesOrNo.NO.value,
      };
      this.personForm = {
        status: CommonStatusEnum.ENABLE
      };
      this.resetForm("form");
      this.resetForm("personForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    showDisabled(val) {
      this.queryParams.status = val ? '' : 0
      this.getList();
    },
    showVirtual(val) {
      this.queryParams.isVirtual = val ? '' : 0
      this.getList();
    },
    showAllChildrenOrg(val) {
      if (val) {
        this.queryParams.fullId = this.currentOrgTreeData.fullId || "/"
        this.queryParams.parentId = null
      } else {
        this.queryParams.fullId = ''
        this.queryParams.parentId = this.currentOrgTreeData.id
      }
      this.getList();
    },
    getOrgKindIds() {
      let kindIds = []
      for (let key in this.queryOrgKindIds) {
        kindIds.push(key)
      }
      return kindIds.join(",")
    },
    showOrg(val) {
      if (val) {
        this.queryOrgKindIds[OrgKindEnum.Org.code] = 1;
      } else {
        delete this.queryOrgKindIds[OrgKindEnum.Org.code]
      }
      this.queryParams.orgKindIds = this.getOrgKindIds();
      this.getList();
    },
    showCenter(val) {
      if (val) {
        this.queryOrgKindIds[OrgKindEnum.Center.code] = 1;
      } else {
        delete this.queryOrgKindIds[OrgKindEnum.Center.code]
      }
      this.queryParams.orgKindIds = this.getOrgKindIds();
      this.getList();
    },
    showDept(val) {
      if (val) {
        this.queryOrgKindIds[OrgKindEnum.Dept.code] = 1;
      } else {
        delete this.queryOrgKindIds[OrgKindEnum.Dept.code]
      }
      this.queryParams.orgKindIds = this.getOrgKindIds();
      this.getList();
    },
    showPos(val) {
      if (val) {
        this.queryOrgKindIds[OrgKindEnum.Position.code] = 1;
      } else {
        delete this.queryOrgKindIds[OrgKindEnum.Position.code]
      }
      this.queryParams.orgKindIds = this.getOrgKindIds();
      this.getList();
    },
    showPsm(val) {
      if (val) {
        this.queryOrgKindIds[OrgKindEnum.Psm.code] = 1;
      } else {
        delete this.queryOrgKindIds[OrgKindEnum.Psm.code]
      }
      this.queryParams.orgKindIds = this.getOrgKindIds();
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增机构按钮操作 */
    handleAddOrg() {
      this.reset();
      if (!this.currentOrgTreeId) {
        this.$modal.msgWarning("请在左侧选择父节点")
        return
      }
      this.form.parentId = this.currentOrgTreeId
      this.form.orgKindId = OrgKindEnum.Org.code
      this.form.orgKindName = OrgKindEnum.Org.name

      this.open = true;
      this.title = "添加机构";
    },
    /** 新增部门按钮操作 */
    handleAddDept() {
      this.reset();
      if (!this.currentOrgTreeId) {
        this.$modal.msgWarning("请在左侧选择父节点")
        return
      }
      this.form.parentId = this.currentOrgTreeId
      this.form.orgKindId = OrgKindEnum.Dept.code
      this.form.orgKindName = OrgKindEnum.Dept.name

      this.open = true;
      this.title = "添加部门";
    },
    /** 新增岗位按钮操作 */
    handleAddPos() {
      this.reset();
      if (!this.currentOrgTreeId) {
        this.$modal.msgWarning("请在左侧选择父节点")
        return
      }
      this.form.parentId = this.currentOrgTreeId
      this.form.orgKindId = OrgKindEnum.Position.code
      this.form.orgKindName = OrgKindEnum.Position.name

      this.open = true;
      this.title = "添加岗位";
    },
    /** 新增人员按钮操作 */
    handleAddPsm() {
      this.reset();
      if (!this.currentOrgTreeId) {
        this.$modal.msgWarning("请在左侧选择岗位点")
        return
      }
      this.personForm.mainOrgId = this.currentOrgTreeId
      this.personForm.parentFullName = this.currentOrgTreeData.fullName

      this.personFormOpen = true;
      this.title = "添加人员";
    },
    //选择人员，添加兼岗
    handleAllocationPsm() {
      this.openUserPicker = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getOrg(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改组织机构管理";
      });
    },
    /** 添加机构 提交按钮 */
    submitOrgForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateOrg(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createOrg(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    submitPersonForm() {
      this.$refs["personForm"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.personForm.id != null) {
          updatePerson(this.personForm).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.personFormOpen = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createPerson(this.personForm).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.personFormOpen = false;
          this.getList();
        });
      });
    },
    // 组织状态修改
    handleStatusChange(row) {
      let text = row.status === CommonStatusEnum.ENABLE ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"组织吗?').then(function () {
        return changeOrgStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE
          : CommonStatusEnum.ENABLE;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除组织机构管理编号为"' + id + '"的数据项?').then(function () {
        return deleteOrg(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有组织机构管理数据项?').then(() => {
        this.exportLoading = true;
        return exportOrgExcel(params);
      }).then(response => {
        this.$download.excel(response, '组织机构管理.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    getUserList(persons) {
      let data = []
      persons.forEach(person => {
        let p = {
          personId: !!person.personId ? person.personId : person.id ,
          code: person.code,
          name: person.name,
          longName: person.name,
          status: CommonStatusEnum.ENABLE,
          isVirtual: YesOrNo.NO.value,
          orgKindId: OrgKindEnum.Psm.code,
          parentId: this.currentOrgTreeData.id
        }
        data.push(p)
      })
      if (data.length > 0) {
        batchCreatePsm(data).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.getList();
        })
      }
    }


  }
};
</script>
